home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3693 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
  2. From: floydb1@lib104.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Converting dates in MMYY to YYMMDD format
  5. Date: 25 Jan 1996 10:28:21 -0500
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Message-ID: <4e87il$pa@lib104.its.rpi.edu>
  8. References: <4e6ms8$ad5@walrus2.walrus.com>
  9. NNTP-Posting-Host: lib104.its.rpi.edu
  10. X-newsreader: xrn 7.04-beta-11
  11.  
  12.  
  13. In article <4e6ms8$ad5@walrus2.walrus.com>, fjordao@walrus.com (Felipe Jordao) writes:
  14. |> 
  15. |> Hi,
  16. |> 
  17. |> I have a rather clumsy function that converts a character string in
  18. |> MMYY format, (such as 1195 for November 1995) into a long DDMMYY
  19. |> format, where DD is always the last day of the month.  Hence "1282"
  20. |> would be converted into 821231 (a long).
  21. |> 
  22. |> Does anyone know a quick way of doing this (around 10-20 lines of
  23. |> code)?
  24. |> 
  25. |> Felipe
  26. |> 
  27.  
  28. One line of (Pseudo)code, should do ( i.e. "result =..." ), using 
  29. a String class:
  30.  
  31. main ()
  32. {
  33.    while ( TRUE )
  34.    {
  35.     String     last_days = "3128..." ;
  36.     String     date ;
  37.     String     result ;
  38.  
  39.     cout     << "Your date (MMYY): " ;
  40.     cin     >> date ;
  41.  
  42.     result     = date.at ( 2, 2 ) +        // ~.at ( from, len )
  43.           date.at ( 0, 2 ) +
  44.           last_days.at ( 
  45.            ( ( atoi ( (char *)date.at ( 2, 2 ) ) - 1 ) * 2 )
  46.            , 2 ) ;
  47.  
  48.     cout     << "YYMMDD" << result_date << endl ;
  49.    }
  50. }
  51.  
  52. You might check to see if the 'date' is four characters long, then
  53. pad to the left (maybe one "0") if is not. 
  54.  
  55.  
  56. Alternatives: 
  57.  
  58. If you don't use a String class, (char *)'s 
  59. should work - replacing "+" with strcat(...) etc.
  60. -- about three lines.
  61.  
  62. Or use (char[]) to fill the result array from (char)'s
  63. in "date" and "last_days" ( i.e. result[0] = date[2] ; 
  64. result[1] = date[3]) and an 'end-of-string' null.
  65. -- about six lines of code.
  66.  
  67. Or convert "date" to (int) and div/mult it, then
  68. add last_days[MONTH-1], where "int last_days[0] = 31 ; etc. 
  69. -- about three lines of code.
  70.  
  71. good luck
  72.  
  73. barry
  74. -- 
  75. +--------------------------------------------------------------------+ 
  76. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  77. | RPI Alum. '84 '87 '88              \\\                             |
  78. +--------------------------------------------------------------------+
  79.